/************************************************************************** 
* ++ 
* Author:			NhatLD
* Module Name:		FolderTree2D.cpp
* 
* Description:		La noi thuc thi ham cua lop CFolderTree2D
* 
* Mod. History:	08.Jun.2013 - NhatLD
*							  File first created 
*							  SimpleGame - Project
*							  Create file
* 
* -- 
************************************************************************/
#include "stdafx.h"
#include "CFolderTree2D.h"


// CFolderTree2D

IMPLEMENT_DYNAMIC(CFolderTree2D, CWnd)

CFolderTree2D::CFolderTree2D()
{
}

CFolderTree2D::~CFolderTree2D()
{
	Gdiplus::GdiplusShutdown(m_gdiplusToken);
}

BEGIN_MESSAGE_MAP(CFolderTree2D, CWnd)
	ON_WM_CREATE()
	ON_WM_MOUSEMOVE()
	ON_WM_PAINT()
END_MESSAGE_MAP()

int CFolderTree2D::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

	return(0);
}

void CFolderTree2D::CreateMainRect(CRect* rect)
{
	RECT rcCalendar;
	GetClientRect (&rcCalendar);

	rect->left		= 10;
	rect->top		= 10;
	rect->bottom	= m_nRow	* ( m_nHeightFolder + 10 );
	rect->right		= m_nColumn * ( m_nWidthFolder + 10 );
}

BOOL CFolderTree2D::CreateFolderTree(int nRow, int nColumn, const RECT& rect, CWnd* pParentWnd)
{
	int nID = 0;

	/* Create window to draw board */
	if (!CWnd::Create(NULL, NULL, WS_VISIBLE, rect, pParentWnd, nID, NULL))
	{
		throw LPCSTR("Calendar::Create window fail !");
		return (FALSE);
	}

	int nLeft	= (rect.right - rect.left - FOLDER_WIDTH) / 2;
	int nRight	= nLeft + FOLDER_WIDTH;
	int nTop	= rect.top + 10;
	int nBottom = nTop + FOLDER_INACTIVE_HEIGHT;

	/* Create folders */
	for(int i = 0; i < ROW_NUM; i++)
	{
		CRect rectFolder = new CRect(nLeft, nTop, nRight, nBottom);
		CBoardCell2D* pBoardCell = new CBoardCell2D(i);

		pBoardCell->CreateBoardCell(rectFolder, this);
		m_arrBoardCell.AddTail(pBoardCell);

		nTop	= nBottom + 10;
		nBottom = nTop + FOLDER_INACTIVE_HEIGHT;
	}
	
	return (TRUE);
}

void CFolderTree2D::DrawFolderTree(Graphics* graphics)
{
	POSITION pos = m_arrBoardCell.GetHeadPosition();
	while(pos)
	{
		CBoardCell2D* curCell = m_arrBoardCell.GetNext( pos );
		if(FALSE == curCell->isClicked())
		{
			curCell->setCellColor(Color::LightCyan);
			curCell->setCellImage(FOLDERIMAGE_INACTIVE);
		}

		curCell->DrawCell(graphics, curCell->getRect());
	}
}

void CFolderTree2D::CalculateFolderTree(int nCellID)
{
	POSITION pos	= m_arrBoardCell.GetHeadPosition();
	int nDistance	= 0;

	while(pos)
	{
		CBoardCell2D* curCell	= m_arrBoardCell.GetNext( pos );

		if(nDistance != 0)
		{
			CRect* rect				= new CRect(curCell->getRect());
			rect->top				+= nDistance;
			rect->bottom			+= nDistance;
			curCell->setRect(rect);
			curCell->MoveWindow(rect);

			delete rect;
		}

		if(curCell->getCellID() == nCellID)
		{
			if(TRUE == curCell->isClicked())
			{
				nDistance = 70;
			}
			else
			{
				nDistance -= 70;
			}
		}
	}

	Invalidate();
}

void CFolderTree2D::OnMouseMove(UINT nFlags, CPoint point)
{
	Invalidate(0);
}

void CFolderTree2D::OnPaint()
{
	CPaintDC dc(this); // Creates a device context for the client area, which
	// also erases the area to be drawn.
	CRect rect;
	GetClientRect(rect);

	HDC Memhdc;
	HBITMAP Membitmap;
	CRect MainRect(rect);

	// Double Buffer
	Memhdc = CreateCompatibleDC(dc.m_hDC);
	Membitmap = CreateCompatibleBitmap(dc.m_hDC, MainRect.Width() , MainRect.Height());
	SelectObject(Memhdc, Membitmap);

	CRect MemRect(0,0, MainRect.Width(), MainRect.Height());
	FillRect(Memhdc, MemRect, CreateSolidBrush(WHITE));
	//Rectangle(Memhdc,MemRect.left, MemRect.top,MemRect.right, MemRect.bottom);
	// GDI+
	Graphics graphics(Memhdc);
	graphics.SetSmoothingMode(SmoothingModeHighQuality);

	// Draw Folder Tree
	DrawFolderTree(&graphics);

	BitBlt(dc, MainRect.left, MainRect.top, MainRect.Width(), MainRect.Height(), Memhdc, 0, 0, SRCCOPY);
	DeleteObject(Membitmap);
	DeleteDC    (Memhdc);
	DeleteDC    (dc);
}
